#include "LRESIndex.h"
#include "LRESScreen.h"
#include "LINFOScreen.h"
#include "../std.h"
#include "../Widgets/Colours.h"
#include "../Widgets/UIBuilder.h"
#include "../Utilities/Convert.h"
#include "../Utilities/trim.h"

LRESIndex::LRESIndex(){
		mProcessor = new LRESProcessor();
		mLayout = createMainLayout(getStringNotIterative(L_APPLICATION), getStringNotIterative(L_UPDATE), " ", getStringNotIterative(L_BACK));
		mItems = (ListBox*)mLayout->getChildren()[1];
		mSoftkey = (Widget*)mLayout->getChildren()[2];
		this->setMain(mLayout);
}

LRESIndex::~LRESIndex(){
	delete mLayout;
}

void LRESIndex::keyPressEvent(int keyCode, int nativeCode)
{
	switch(keyCode)
	{
		case MAK_UP:
		case MAK_2:
			mItems->selectPreviousItem();
			break;
		case MAK_DOWN:
		case MAK_8:
			mItems->selectNextItem();
			break;
		case MAK_SOFTRIGHT:
		case MAK_6:
			APPCONTROLLER->show("MainScreen");
			break;
		case MAK_FIRE:
			LRESItemWidget* widget = (LRESItemWidget*)mItems->getChildren()[mItems->getSelectedIndex()];
			APPCONTROLLER->setCompanyId(widget->getId());
			if (mIsToday == 1){
				LINFOScreen* scr = (LINFOScreen*)APPCONTROLLER->getScreen("LINFOScreen");
				scr->setCompanyId(widget->getId());
				scr->setCompanyName(widget->getCompanyName());
				scr->setIsGetData(false);
				APPCONTROLLER->show("LINFOScreen");
			}else{
				LRESScreen* screen= (LRESScreen*)APPCONTROLLER->getScreen("LRESScreen");
				screen->indexAction = 0;
				LRESItem* newitem = new LRESItem();
				newitem->companyid = widget->getId();
				newitem->companyname = widget->getCompanyName();
				newitem->drawdate = widget->getDrawDate();
				newitem->istoday = widget->getIsToday();
				newitem->outofdate = widget->getIsOod();
				newitem->result = widget->getResult();
				newitem->shortcode = widget->getShortCode();
				newitem->smscnt = widget->getSmsCnt();
				screen->setLRESItem(newitem);
				delete newitem;
				APPCONTROLLER->show("LRESScreen");
			}
			break;
		case MAK_SOFTLEFT:
			if (mItems->getChildren()[0] != NULL){
				mItems->setSelectedIndex(0);
			}
			mItems->clear();
			APPCONTROLLER->mLRESList.clear();
			Label* l = (Label*)mSoftkey->getChildren()[1];
			mItems->setEnabled(false);
			l->setCaption("Loading");
			l->update();
			mProcessor->addRequest(APPCONTROLLER->getUrl(), this);
			break;
	}
}

void LRESIndex::itemReceived(LRESItem& item){
	int itemToday = stringToInteger(item.istoday);

	APPCONTROLLER->mLRESList.add(item);

	if (mIsToday == itemToday){
		LRESItemWidget* newItem = new LRESItemWidget(0, 0, APPCONTROLLER->getScreenWidth(), HEIGHTITEM, mItems);

		newItem->setCompanyName(item.companyname);
		newItem->setCompanyFont(APPCONTROLLER->getFont("default"));
		newItem->setDrawDate(item.drawdate);
		newItem->setDrawDateFont(APPCONTROLLER->getFont("default"));

		newItem->setId(item.companyid);
		newItem->setSmsCnt(item.smscnt);
		newItem->setShortCode(item.shortcode);
		newItem->setResult(item.result);
		newItem->setIsToday(item.istoday);
		newItem->setIsOod(item.outofdate);

		newItem->setSkin(APPCONTROLLER->getSkin("default"));
		//newItem->addWidgetListener(this);

		mItems->setSelectedIndex(0);
		mItems->getChildren()[0]->setSelected(true);
	}
}

void LRESIndex::triggered(Widget* sender)
{
	//Act as if fire has been pressed
	//keyPressEvent(MAK_FIRE, 0);
}

void LRESIndex::pointerReleaseEvent(MAPoint2d point){

}

void LRESIndex::pointerPressEvent(MAPoint2d point){
	Point p;
	p.set(point.x, point.y);

	if (mItems->contains(p)){
		for(int i=0; i< mItems->getChildren().size(); i++){
			if (mItems->getChildren()[i]->contains(p)){
				int index = mItems->getSelectedIndex();
				if (index == i) keyPressEvent(MAK_FIRE, 0);
				else mItems->setSelectedIndex(i);
				break;
			}
		}
	}
	else if (mSoftkey->contains(p)){
		if(mSoftkey->getChildren()[0]->contains(p)) keyPressEvent(MAK_SOFTLEFT, 0);
		else if (mSoftkey->getChildren()[2]->contains(p)) keyPressEvent(MAK_SOFTRIGHT, 0);
	}
}

void LRESIndex::setIsToday(int today){
	mIsToday = today;
	Label* l = (Label*)mLayout->getChildren()[0];
	String title = "";
	if (today == 0) title = getStringIterative(MAIN_TEXT_START + 1); else title = getStringIterative(MAIN_TEXT_START);
	l->setCaption(title);
	l->update();
	if (mItems->getChildren()[0] != NULL) mItems->setSelectedIndex(0);
	mItems->clear();

	if (APPCONTROLLER->mIsGetLRES && (APPCONTROLLER->mLRESList.size() > 0)){
		//lprintfln("just have item");
		for(int i = 0; i< APPCONTROLLER->mLRESList.size(); i++){
			LRESItem item = APPCONTROLLER->mLRESList[i];
			int itemToday = stringToInteger(item.istoday);
			if (itemToday == mIsToday){
				LRESItemWidget* newItem = new LRESItemWidget(0, 0, APPCONTROLLER->getScreenWidth(), HEIGHTITEM, mItems);

				newItem->setCompanyName(item.companyname);
				newItem->setCompanyFont(APPCONTROLLER->getFont("default"));
				newItem->setDrawDate(item.drawdate);
				newItem->setDrawDateFont(APPCONTROLLER->getFont("default"));

				newItem->setId(item.companyid);
				newItem->setSmsCnt(item.smscnt);
				newItem->setShortCode(item.shortcode);
				newItem->setResult(item.result);
				newItem->setIsToday(item.istoday);
				newItem->setIsOod(item.outofdate);

				newItem->setSkin(APPCONTROLLER->getSkin("default"));
				//newItem->addWidgetListener(this);

				mItems->setSelectedIndex(0);
				mItems->getChildren()[0]->setSelected(true);
			}
		}
	}
}

void LRESIndex::processingComplete(){
	APPCONTROLLER->mIsGetLRES  = true;
	Label* l = (Label*)mSoftkey->getChildren()[1];
	l->setCaption(" ");
	l->update();
	mItems->setEnabled(true);
	lprintfln("process LRES complete");
}

void LRESIndex::show(){
	if (!APPCONTROLLER->mIsGetLRES){
		Label* l = (Label*)mSoftkey->getChildren()[1];
		l->setCaption("Loading");
		l->update();
		mItems->setEnabled(false);
		mProcessor->addRequest(APPCONTROLLER->getUrl(), this);
	}else if (mItems->getChildren()[0] != NULL){
		mItems->setSelectedIndex(0);
		mItems->getChildren()[0]->setSelected(true);
	}
	Screen::show();
}
